package be.ipl.finito.threads;

import java.util.Map;
import java.util.Set;

import javax.ejb.EJB;
import javax.servlet.ServletContext;

import be.ipl.finito.domaine.Participation;
import be.ipl.finito.domaine.Partie;
import be.ipl.finito.exceptions.BizException;
import be.ipl.finito.usecases.GestionParties;
import be.ipl.finito.util.Consts;

public class TimeOutJouer extends Thread {
	public static final long EXPIRED = Consts.TEMPS_INACTIVITE;

	private ServletContext sc;
	private Participation p;
	private Partie part;
	private int threadID;
	public TimeOutJouer(ServletContext sc,Participation p,Partie part,int threadID) {
		this.sc = sc;
		this.p = p;
		this.part = part;
		this.threadID = threadID;
	}
	@Override
	public void run() {
		super.run();
		try {
			sleep(EXPIRED);
			Object currentThread = sc.getAttribute("participation"+p.getId());
			int currentThreadID = 0;
			if(currentThread != null){
			currentThreadID = (Integer)currentThread;	
			}
			if(currentThreadID == 0 || threadID == currentThreadID){
				Map<Partie,Set<Participation>> parties = (Map<Partie, Set<Participation>>) sc.getAttribute("parties");
				Set<Participation> participations = parties.get(part);
				participations.remove(p);
				parties.put(part, participations);
			}
			System.out.println("fini");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
